import { Action, Actor } from 'iflux2';
import {ArticleTypes} from '../constants/actionTypes.js'
export default class AricleActor extends Actor {
    defaultState () {
        return {
            articles:[]
        }
    }

    /**
     * 获取文章列表
     * @param {*} state 
     * @param {*} list 
     */
    @Action(ArticleTypes.FETCH_ARTICLES)
    fetchArticles(state,list){
        return state.set('articles',list);
    }

    /**
     * 添加文章
     * @param {*} state 
     * @param {*} section 
     */
    @Action(ArticleTypes.ADD_ARTICLE)
    addArticle(state,section){
        return state.update('articles',articles=>articles.push(section))
    }

    /**
     * 修改文章
     * @param {*} state 
     * @param {*} section 
     */
    @Action(ArticleTypes.UPDATE_ARTICLE)
    updateArticle(state,section){
        return state.update('articles',articles => {
            return articles.map(item=>{
                if(item.get('id') === section.get('id')){
                    return item.merge(section)
                }
                return item;
            })
        })
    }
    /**
     * 删除文章
     * @param {*} state 
     * @param {*} id 
     */
    @Action(ArticleTypes.DELETE_ARTICLE)
    deleteArticle(state,id){
        return state.update('articles',articles=>articles.filter(item=>item.get('id') !== id))
    }
}